package com.deruan.hotcore.resource.action;

import java.awt.event.ActionEvent;

import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.command.CommandProcessor;
import org.columba.core.gui.action.AbstractColumbaAction;
import org.columba.mail.command.MailFolderCommandReference;
import org.columba.mail.gui.frame.MailFrameMediator;
import org.columba.mail.gui.message.action.ClearMessageViewerAction;
import org.columba.mail.gui.message.command.ViewMessageCommand;

import com.deruan.hotcore.resource.command.ResultCommandReference;
import com.deruan.hotcore.resource.command.ViewResourceCommand;
import com.deruan.hotcore.resource.gui.tree.ResultTreeNode;

public class ViewResourceAction extends AbstractColumbaAction
{
	protected static Object oldUid;

	/**
	 * @param controller
	 */
	public ViewResourceAction(IFrameMediator controller) {
		super(controller, "ViewResourceAction");
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent evt) 
	{	
		if(evt==null)
		{
			MailFolderCommandReference references = (MailFolderCommandReference) ((MailFrameMediator) getFrameMediator()).getResourceTableSelection();
	        Object[] uids = references.getUids();
	        if (uids.length == 1) 
	        {
				// show selected message
				CommandProcessor.getInstance().addOp(new ViewResourceCommand(getFrameMediator(), references));
			} 
			else 
			{
				   // clear message viewer
				 new ClearMessageViewerAction(getFrameMediator()).actionPerformed(evt);
			 } 
		}
		else {
			ResultCommandReference references = (ResultCommandReference) ((MailFrameMediator) getFrameMediator()).getResultTreeSelection();
			ResultTreeNode node = references.getSelectedNode();
	        if (node!=null) 
	        {
				// show selected message
				CommandProcessor.getInstance().addOp(new ViewResourceCommand(getFrameMediator(), references));
			} 
			else 
			{
				   // clear message viewer
				 new ClearMessageViewerAction(getFrameMediator()).actionPerformed(evt);
			 } 
		}

	}
}

